<?php

/**
 *  机型逻辑
*/
namespace app\admin\service;
use app\admin\model\Images;
use app\admin\model\SellerInfo as ModelSeller;
use app\facade\Basic;

class Seller extends BaseService
{


    /**
     *   获取枚举
    */
    public function getEnum($param)
    {
       $modelSeller = new ModelSeller();
       $result = $modelSeller->getFieldEnum($param['field']);
        return Basic::data(0,'ok',$result);
    }

    /**
     * 获取机型列表
     */
    public function getList($param)
    {

        $where[] = ['is_del', '=',0];
        if (!empty($param['name'])) {
            $where[] = ['name', 'like', '%' . $param['name'] . '%'];
        }

        if (!empty($param['category'])) {
            $category[] = 3;
            $category[] = $param['category'];
            $where[] = ['category', 'in',$category];
        }

        if ( $param['category'] == 1 ) {
            if (!empty($param['sup_type'])) {
                $where[] = ['sup_type', '=',$param['sup_type']];
            }
        }

        if (!empty($param['phone'])) {
            $param['phone'] = trim($param['phone']);
            $where[] = ['phone', 'like', '%' . $param['phone'] . '%'];
        }


        if (!empty($param['email'])) {
            $param['email'] = trim($param['email']);
            $where[] = ['email', 'like', '%' . $param['email'] . '%'];
        }

        $result = ModelSeller::with(['owner_name'=>function($query){
            return $query->field('id,cname');
        },'creator_name'=>function($query){
            return $query->field('id,cname');
        },'updater_name'=>function($query){
            return $query->field('id,cname');
        }])
            ->where($where)
            ->field('id,name,domain,sup_type,phone,create_time,owner,creator,update_time,updater,contact')
            ->order('id', 'desc')
            ->paginate([
                'list_rows'=> $param['pageSize'],
                'var_page' => 'pageIndex',
            ]);


        $data = $result->toArray();


        foreach ( $data['data'] as $key =>$value ) {

            /* 负责人 */
            if(!empty($value['creator_name'])) {
                $data['data'][$key]['creator_name'] = $value['creator_name']['cname'];
            } else {
                $data['data'][$key]['creator_name'] = '';
            }

            /* 更新人 */
            if(!empty($value['updater_name'])) {
                $data['data'][$key]['updater_name'] = $value['updater_name']['cname'];
            } else {
                $data['data'][$key]['updater_name'] = '';
            }

            if(isset($value['owner_name'])) {
                $data['data'][$key]['owner_name'] = $value['owner_name']['cname'];
            } else {
                $data['data'][$key]['owner_name'] = '';
            }

        }

        return Basic::paginator(0, 'ok', $data);
    }


    /**
     * save
     */
    public function save($param)
    {

        $param['name'] = trim($param['name']);
        $result = ModelSeller::where('name', '=', $param['name'])->where('is_del','=',0)->find();

        if ($result) {
            return Basic::data(1, '记录已存在');
        }

        $data = $param;
        $data['create_time'] = date('Y-m-d H:i:s',time());

        if ( $data['partner'] ) {
            $data['partner'] = implode(', ', $data['partner']);
        }

        if ( $data['att_path'] ) {
            $data['att_path'] = implode(', ', $data['att_path']);
        }

        if(empty($data['country'])) {
             unset($data['country']);
        }

        if ( empty($data['owner']) ) {
            $data['owner'] = NULL;
        }
        $result = ModelSeller::create($data);
        if ($result) {
            $this->logger->logOperation($result->id, 'Seller', '添加', '添加供应商');
            return Basic::data(0, '保存成功');
        } else {
            return Basic::data(1, '保存失败');
        }
    }


    /**
     *  修改机型
     */
    public function update($param)
    {

        $param['name'] = trim($param['name']);
        $data = $param;
        $data['update_time'] = date('Y-m-d H:i:s',time());

        if ( $data['partner'] ) {
            $data['partner'] = implode(', ', $data['partner']);
        }

        if ( $data['att_path'] ) {
            $data['att_path'] = implode(', ', $data['att_path']);
        }

        if (ModelSeller::where('name', '=', $param['name'])->where('id','<>', $param['id'])->where('is_del','=',0)->find()) {
            return Basic::data(1, '已存在');
        }

        $result = ModelSeller::where('id','=', $param['id'])->where('is_del','=',0)->find();
        if (!$result) {
            return Basic::data(1, 'ID 不存在');
        }

        foreach ($data as $key => $value) {
            $result->$key = $value;
        }

        if ( empty($data['owner']) ) {
            $data['owner'] = NULL;
        }

        if ($result->save()) {
            $this->logger->logOperation($result->id, 'Seller', '修改', '修改供应商');
            return Basic::data(0, '修改成功');
        } else {
            return Basic::data(1, '修改失败');
        }
    }


    /**
     *  获取详情
    */
    public function detail($param)
    {
        $result = ModelSeller::with(['owner_name'=>function($query){
            return $query->field('id,cname');
        },'creator_name'=>function($query){
            return $query->field('id,cname');
        },'updater_name'=>function($query){
            return $query->field('id,cname');
        }])
            ->where('id','=',$param['id'])
            ->where('is_del','=',0)
            ->field('id,name,domain,category,sup_type,phone,email,create_time,qq,wechat,dingtalk,is_deal,whatsapp,att_path,address,country,tags,owner,owner,partner,creator,update_time,att_path,updater,contact,remark')
            ->find();

        if ( !$result ) {
            return Basic::data(1, '未找到');
        }

        $data = $result->toArray();

        $data['is_deal'] = (int) $data['is_deal'];

        /* 负责人 */
        if(isset($data['creator_name'])) {
            $data['creator_name'] = $data['creator_name']['cname'];
        } else {
            $data['creator_name'] = '';
        }

        /* 更新人 */
        if(isset($data['updater_name'])) {
            $data['updater_name'] = $data['updater_name']['cname'];
        } else {
            $data['updater_name'] = '';
        }
        if(isset($data['owner_name'])) {
            $data['owner_name'] = $data['owner_name']['cname'];
        } else {
            $data['owner_name'] = '';
        }

        /* 合作人 */
        if ( !empty($data['partner']) ) {
            $partner = explode(',', $data['partner']);
            if($partner) {
                $data['partner'] = array_map('intval', $partner);
            } else {
                $data['partner'] = [];
            }
        } else {
            $data['partner'] = [];
        }


        /* 附件 */
        if ( !empty($data['att_path']) ) {
            $images = new Images();
            $att_path = explode(',', $data['att_path']);
            $data['att_path'] = $images->getImagesByIds($att_path);
        } else {
            $data['att_path'] = [];
        }

        if ( $result ) {
            return Basic::data(0, 'ok', $data);
        } else {
            return Basic::data(1, '获取失败');
        }
    }

    /**
     *   删除机型
    */
    public function delete($param)
    {
        $result = ModelSeller::where('id','in',$param['ids'])->where('is_del','=',0)->select();
        if(!$result) {
            return Basic::data(1, '未找到');
        }

        if ( $result ) {
            $result = ModelSeller::where('id','in',$param['ids'])->where('is_del','=',0)->update([
                'updater' => $param['updater'],
                'update_time' => date('Y-m-d H:i:s',time()),
                'is_del' => time()
            ]);

            if ( $result ) {
                return Basic::data(0, '删除成功');
            } else {
                return Basic::data(1, '删除失败');
            }
        } else {
            return Basic::data(1, '删除失败');
        }
    }
}
